//
//  GYKey.m
//  LockManagement
//
//  Created by apple on 2018/7/25.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYKey.h"
#import "GYNetWorkTools.h"

@implementation GYKey

+ (void)getKeyListsCurrent:(NSString *)current pageSize:(NSString *)pageSize success:(void (^)(NSArray *))success error:(void (^)(NSError *))failed {
    NSMutableArray *tempArray = [NSMutableArray array];
    NSString *IP = GETOBJ(@"IP")
    NSString *port = GETOBJ(@"port")
    [SVProgressHUD showWithStatus:@"正在加载钥匙列表..."];
    [GYNetWorkTools requestGetKeysListWithURL:[NSString stringWithFormat:@"http://%@:%@/api/getKeysList",IP,port] current:current pageSize:pageSize success:^(NSDictionary *obj) {
        NSLog(@"obj=============%@",obj);
        [SVProgressHUD dismiss];
        int state = [[obj objectForKey:@"state"] intValue];
        if (state == 0) {
            NSArray *data = [obj objectForKey:@"data"];
            for (NSDictionary *tempDic in data) {
                GYKey *key = [[GYKey alloc] initWithDic:tempDic];
                [tempArray addObject:key];
            }
            success(tempArray);
            
            //2.把加载的钥匙列表储存在沙盒中
            NSMutableArray *tempKeys = [NSMutableArray array];
            for (GYKey *model in tempArray) {
                NSData *keyData = [NSKeyedArchiver archivedDataWithRootObject:model];
                [tempKeys addObject:keyData];
            }
            NSString *filePath = [NSString CachesDir:@"keys.plist"];
            [tempKeys writeToFile:filePath atomically:YES];
        }else {
            NSArray *models = [self getCachesKeys];
            success(models);
        }
        
    } error:^(NSError *error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"网络加载数据出错,请稍后加载!"];
        NSArray *models = [self getCachesKeys];
        success(models);
        failed(error);
        NSLog(@"get keys list   error=======%@",error);
    }];
}

+ (NSArray *)getCachesKeys {
    //加载数据失败  要加载缓存数据
    NSString *filePath = [NSString CachesDir:@"keys.plist"];
    NSArray *temp = [NSArray arrayWithContentsOfFile:filePath];
    NSMutableArray *models = [NSMutableArray array];
    for (NSData *data in temp) {
        GYKey *key = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        [models addObject:key];
    }
    
    return models;
}

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

#pragma mark -- 归档

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_keyName forKey:@"keyName"];
    [aCoder encodeInt:_keyID forKey:@"keyID"];
    [aCoder encodeInt:_keyState forKey:@"keyState"];
    [aCoder encodeObject:_keyMac forKey:@"keyMac"];
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        _keyName = [aDecoder decodeObjectForKey:@"keyName"];
        _keyID = [aDecoder decodeIntForKey:@"keyID"];
        _keyMac = [aDecoder decodeObjectForKey:@"keyMac"];
        _keyState = [aDecoder decodeIntForKey:@"keyState"];
    }
    return self;
}


@end
